package ar.com.larreta.reportes;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.log4j.Logger;

public abstract class TipoReporte {
	
	/** The Constant log. */
	private static final Logger log = Logger.getLogger(TipoReporte.class);

	protected UtilidadesReportes reportsUtils;
	
	private  String path;
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public TipoReporte(UtilidadesReportes reportsUtils, String path){
		this.path = path;
		this.reportsUtils = reportsUtils;
	}
	
	/**
	 * Se obtiene el objeto print que permite imprimir el reporte en diferentes formatos
	 * @param dataSource
	 * @return
	 * @throws IOException 
	 */
	protected JasperPrint getPrint(JRDataSource dataSource) throws IOException {
		JasperPrint print = null;
		try {
			InputStream reportStream = this.getClass().getClassLoader().getResourceAsStream(path);
			JasperReport jr = JasperCompileManager.compileReport(reportStream);
		    print = JasperFillManager.fillReport(jr, null, dataSource);
		    log.debug("JasperPrint object filled with data");
		}
		catch (JRException e) {
			log.error(e);
		} 
		return print;
	}
	
	public ByteArrayOutputStream getOutputStream(Collection dataSource)throws IOException{
		return getOutputStream(new JRBeanCollectionDataSource(dataSource));
	}
	
	public abstract ByteArrayOutputStream getOutputStream(JRDataSource dataSource)throws IOException;
	public abstract String getContentType();
}
